package com.ssy.lingxi.report.serviceimpl;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.api.vo.response.ActiveMerchantVO;
import com.ssy.lingxi.report.dao.merchantmember.MemberDAO;
import com.ssy.lingxi.report.dao.order.ProcurementOrderDAO;
import com.ssy.lingxi.report.entity.report.ActiveMerchantDO;
import com.ssy.lingxi.report.model.bo.MemberBO;
import com.ssy.lingxi.report.model.bo.MemberOrderBO;
import com.ssy.lingxi.report.repository.report.ActiveMerchantRepository;
import com.ssy.lingxi.report.service.IActiveMerchantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活跃商户接口实现
 * @author 伍将
 * @date 2020/11/2
 * @version 2.0.0
 */

@Service
public class ActiveMerchantServiceImpl implements IActiveMerchantService {

    private static final Logger logger = LoggerFactory.getLogger(ActiveMerchantServiceImpl.class);

    @Resource
    private ActiveMerchantRepository repository;

    @Resource
    private ProcurementOrderDAO procurementOrderDAO;

    @Resource
    private MemberDAO memberDAO;


    /**
     * 获取活跃商户
     * @author 伍将
     * @date 2020/11/2
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.report.model.vo.response.ActiveMerchantVO>>
     **/
    @Override
    public Wrapper<List<ActiveMerchantVO>> getActiveMerchantList() {

        List<ActiveMerchantVO> resultList = repository.findAll().stream().map(r -> {
            ActiveMerchantVO query = new ActiveMerchantVO();
            query.setMemberId(r.getMemberId());
            query.setRoleId(r.getRoleId());
            query.setMemberShopId(r.getMemberShopId());
            query.setMemberName(r.getMemberName());
            query.setMemberLogo(r.getMemberLogo());

            return query;
        }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }

    /**
     * 活跃商户任务
     * @author 伍将
     * @date 2020/11/4
     * @param startTime:
     * @param count:
     **/
    @Override
    public void ActiveMerchantTask(Long startTime, Integer count) {

        // step 1：查询活跃商户
        List<MemberOrderBO> boList = procurementOrderDAO.getOrderTopPurchaseMember(startTime, count);
        if (boList == null || boList.size() <= 0) {
            logger.info("没有查询到活跃商户");
            return;
        }
        logger.info("查询到活跃商户:{}条", boList.size());

        List<Long> memberIds = new ArrayList<>();

        List<ActiveMerchantDO> doList = boList.stream().map(r -> {

            memberIds.add(r.getMemberId());

//            MemberIdRoleIdVO shopParam = new MemberIdRoleIdVO();
//            shopParam.setMemberId(r.getMemberId());
//            shopParam.setRoleId(r.getRoleId());


            ActiveMerchantDO entity = new ActiveMerchantDO();
            entity.setMemberId(r.getMemberId());
            entity.setRoleId(r.getRoleId());
            return entity;
        }).collect(Collectors.toList());

        // step 2：查询会员名称
        List<MemberBO> memberBOList = memberDAO.getMemberList(memberIds);
        if (memberBOList == null || memberBOList.size() <= 0) {
            logger.info("没有查询到对应会员");
            return;
        }

        // step 3：查询店铺信息

        // step 4：组装数据
        doList = doList.stream().peek(r -> {
            MemberBO member = memberBOList.stream().filter(o -> o.getId().equals(r.getMemberId())).findFirst().orElse(null);
            if (member != null) {
                r.setMemberName(member.getName());
                r.setMemberLogo(member.getLogo());
                r.setMemberShopId(0L);
            }

        }).filter(o -> o.getMemberLogo() != null).collect(Collectors.toList());

        // step 5：数据入库
        if (doList.size() <= 0) {
            logger.info("没有需要更新的活跃商户");
        } else {
            logger.info("需要更新的活跃商户:{}条", doList.size());
            repository.deleteAll();
            repository.saveAll(doList);
        }
    }
}
